package com.gubatian.jdk.analyse.register.client;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import com.gubatian.jdk.analyse.register.client.ClientCachedServiceRegistry.*;

/**
 * 发送注册请求
 * @author gubatian
 * @date 2021/9/7 4:06 下午
 */
public class HttpSender {
    /**
     * 发送注册请求
     * @param request 请求
     * @return 响应
     */
    public RegisterResponse register(RegisterRequest request) {
        // 实际上会基于类似HttpClient这种开源的网络包
        // 你可以去构造一个请求，里面放入这个服务实例的信息，比如服务名称，ip地址，端口号
        // 然后通过这个请求发送过去
        System.out.println("服务实例【" + request + "】，发送请求进行注册......");

        // 收到register-server响应后，封装一个RegisterResponse对象
        RegisterResponse response = new RegisterResponse();
        response.setStatus(RegisterResponse.SUCCESS);
        return response;
    }

    /**
     * 发送心跳请求
     * @param request
     * @return
     */
    public HeartbeatResponse heartbeat(HeartbeatRequest request) {
        System.out.println("服务实例【" + request.getServiceInstanceId() + "】，发送心跳请求......");
        HeartbeatResponse response = new HeartbeatResponse();
        response.setStatus(HeartbeatResponse.SUCCESS);
        return response;
    }

    public Applications fetchFullRegistry() {
        Map<String, Map<String, ServiceInstance>> registry = new HashMap<>();

        ServiceInstance serviceInstance = new ServiceInstance();
        serviceInstance.setHostname("finance-service-01");
        serviceInstance.setIp("192.168.31.1207");
        serviceInstance.setPort(9000);
        serviceInstance.setServiceInstanceId("FINANCE-SERVICE-192.168.31.207:9000");
        serviceInstance.setServiceName("FINANCE-SERVICE");

        Map<String, ServiceInstance> serviceInstances = new HashMap<String, ServiceInstance>();
        serviceInstances.put("FINANCE-SERVICE-192.168.31.207:9000", serviceInstance);

        registry.put("FINANCE-SERVICE", serviceInstances);

        System.out.println("拉取注册表：" + registry);
        return new Applications(registry);
    }

    /**
     * 增量拉取注册表
     */
    public DeltaRegistry fetchDeltaRegistry() {
        LinkedList<RecentlyChangedServiceInstance> recentlyChangedQueue = new LinkedList<>();

        ServiceInstance serviceInstance = new ServiceInstance();
        serviceInstance.setHostname("order-service-01");
        serviceInstance.setIp("192.168.31.288");
        serviceInstance.setPort(9000);
        serviceInstance.setServiceInstanceId("ORDER-SERVICE-192.168.31.288:9000");
        serviceInstance.setServiceName("ORDER-SERVICE");

        RecentlyChangedServiceInstance recentlyChangedItem = new RecentlyChangedServiceInstance(
                serviceInstance,
                System.currentTimeMillis(),
                ServiceInstanceOperation.REGISTER);

        recentlyChangedQueue.offer(recentlyChangedItem);

        System.out.println("增量拉取注册表：" + recentlyChangedQueue);

        DeltaRegistry deltaRegistry = new DeltaRegistry(recentlyChangedQueue, 2L);
        return deltaRegistry;
    }
    /**
     * 服务下线
     * @param serviceName 服务名称
     * @param serviceInstanceId 服务实例ID
     */
    public void cancel(String serviceName, String serviceInstanceId) {
        System.out.println("服务实例下线【" + serviceName + ", " + serviceInstanceId + "】");
    }
}
